<?php
function do_Error($aString)
{
  header("Content-Type: text/html");
?>
<head>
  <title>Error Processing Form</title>

<body>
  <h1>Error Processing Form</h1>

  <p>
<?php
  print($aString);

  if (isset($_GLOBALS["tempdir"]) && $_GLOBALS["tempdir"] != "")
      exec("rm -rf \"" . $_GLOBALS["tempdir"] . "\"");

  exit;
}

function check_file($aName, $aSuffix) {
  if (!isset($_FILES[$aName]))
    do_Error("." . $aSuffix . " file not attached.");

  $pattern = '/\.' . $aSuffix . '$/';

  if ($_FILES[$aName]["error"])
    do_Error("Error uploading ." . $aSuffix . " file.");

  if (!preg_match($pattern, $_FILES[$aName]["name"]))
    do_Error(".dic filename does not end in .dic");
}

$locale_pattern = '/^[a-z]{2,3}(-[A-Z]{2,3})?(-[a-z]{2,3})?$/';
if (!preg_match($locale_pattern, $_POST["langcode"]))
  do_Error("Language code must be ab[-CD[-modifier]]");

check_file("dicfile", "dic");
check_file("afffile", "aff");

$tempdir = exec("mktemp -d", $lines, $retval);
if ($retval)
  do_Error("Couldn't create temporary directory.");

$installrdf = fopen($tempdir . "/install.rdf", "w");

// It's possible to use MySpell dictionaries in new
// versions that support HunSpell, so maxVersion is
// the same for both dictionary types.
$fx_maxversion = "3.0.*";
$tb_maxversion = "3.0a2pre";
$sm_maxversion = "2.0a1";

// Set minVersion according to the dictionary type
if ($_POST["type"] == "myspell") {
  $fx_minversion = "2.0b1";
  $tb_minversion = "2.0a1";
  $sm_minversion = "1.1a";
} else {
  $fx_minversion = "3.0a8pre";
  $tb_minversion = "3.0a1pre";
  $sm_minversion = "2.0a1pre";
}

$rdfformat = <<<EP
<?xml version="1.0"?>

<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:install-manifest">
    <em:id>%s@dictionaries.addons.mozilla.org</em:id>
    <em:version>%s</em:version>
    <em:targetApplication>
      <Description>
        <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
        <em:minVersion>$fx_minversion</em:minVersion>
        <em:maxVersion>$fx_maxversion</em:maxVersion>
      </Description>
    </em:targetApplication>
    <em:targetApplication>
      <Description>
        <em:id>{3550f703-e582-4d05-9a08-453d09bdfdc6}</em:id>
        <em:minVersion>$tb_minversion</em:minVersion>
        <em:maxVersion>$tb_maxversion</em:maxVersion>
      </Description>
    </em:targetApplication>
    <em:targetApplication>
      <Description>
        <em:id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</em:id>
        <em:minVersion>$sm_minversion</em:minVersion>
        <em:maxVersion>$sm_maxversion</em:maxVersion>
      </Description>
    </em:targetApplication>
    <em:name>%s</em:name>
  </Description>
</RDF>
EP;

fwrite($installrdf,
       sprintf($rdfformat,
               $_POST["langcode"], $_POST["version"], $_POST["desc"]));

fclose($installrdf);

$installjs = fopen($tempdir . "/install.js", "w");

$jsformat = <<<EP
var err = initInstall("%s", "%s@dictionaries.addons.mozilla.org", "%s");
if (err != SUCCESS)
    cancelInstall();

var fProgram = getFolder("Program");
err = addDirectory("", "%s@dictionaries.addons.mozilla.org",
		   "dictionaries", fProgram, "dictionaries", true);
if (err != SUCCESS)
    cancelInstall();

performInstall();
EP;

fwrite($installjs,
       sprintf($jsformat,
	       $_POST["desc"], $_POST["langcode"], $_POST["version"],
	       $_POST["langcode"]));

fclose($installjs);

if (!mkdir($tempdir . "/dictionaries"))
    do_Error("Internal error creating dictionaries/ directory.");

move_uploaded_file($_FILES["dicfile"]["tmp_name"],
                   $tempdir . "/dictionaries/" . $_POST["langcode"] . ".dic");
move_uploaded_file($_FILES["afffile"]["tmp_name"],
                   $tempdir . "/dictionaries/" . $_POST["langcode"] . ".aff");

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" .
	$_POST["langcode"] . "-dictionary.xpi\"");

$command = "cd \"". $tempdir . "\" && zip -r - .";

passthru($command);

exec("rm -rf \"" . $_GLOBALS["tempdir"] . "\"");
?>
